package game.entity.ground;

import edu.monash.fit2099.engine.Exit;
import edu.monash.fit2099.engine.GameMap;
import edu.monash.fit2099.engine.Ground;
import edu.monash.fit2099.engine.Location;
import game.util.RandomUtil;

import java.util.List;

/**
 * A class that represents bare dirt.
 */
public class Dirt extends Ground {
    private final int GROW_BUSH_RATE = 1;
    private final int ADVANCED_GROW_BUSH_RATE = 10;

    public Dirt() {
        super('.');
    }

    @Override
    public void tick(Location location) {
        GameMap map = location.map();
//        随机并且周围不能有树时生成bush
        if (RandomUtil.shotOn(GROW_BUSH_RATE) && !aroundTree(location)) {
            location.setGround(new Bush());
        }
        int count = 0;
        List<Exit> exits = location.getExits();
        for (Exit exit : exits) {
            if(exit.getDestination().getGround() instanceof Bush){
                count++;
            }
        }
        if(count>=2 && RandomUtil.shotOn(ADVANCED_GROW_BUSH_RATE) && !aroundTree(location)){
            location.setGround(new Bush());
        }

    }

    @Override
    public boolean aroundTree(Location location) {
        GameMap map = location.map();
        List<Exit> exits = location.getExits();
        for (Exit exit : exits) {
            if(exit.getDestination().getGround() instanceof Tree){
                return true;
            }
        }
        return false;
    }
}
